package cn.kgc.vue.controller;

import cn.kgc.vue.annotation.LogAnnotation;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Schedule;
import cn.kgc.vue.service.ScheduleService;
import cn.kgc.vue.enums.OperationType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("class")
@Api(tags = "班级模块")
public class ClassController {
@Resource
   private ScheduleService scheduleService;


    @PostMapping("clazzPage")
    @ApiOperation("班级分页")
    public ResponseResult clazzPage(@RequestBody QueryCondition<Schedule> queryCondition){
        return scheduleService.clazzPage(queryCondition);
    }

    @LogAnnotation(model="班级模块",operation= OperationType.DELETE)
    @RequiresPermissions("base:clazz:delete")
    @DeleteMapping("deleteClass/{id}")
    @ApiOperation("删除班级")
    public ResponseResult deleteClass(@PathVariable Integer id){
        scheduleService.removeById(id);
        return ResponseResult.success().message("删除成功");
    }

    @LogAnnotation(model="班级模块",operation= OperationType.ADD)
    @RequiresPermissions(value = {"base:clazz:add","base:clazz:update"},logical = Logical.AND)
    @PostMapping("addOrUpDataClass")
    @ApiOperation("添加or修改班级")
    public ResponseResult addOrUpDataClass(@RequestBody Schedule schedule){

        if(schedule.getId()==null){
            boolean b = scheduleService.saveOrUpdate(schedule);
            if (!b){
                return ResponseResult.fail().message("请刷新后重试");
            }
            return ResponseResult.success().message("新增成功");
        }
        boolean b = scheduleService.saveOrUpdate(schedule);
        if (!b){
            return ResponseResult.fail().message("请刷新后重试");
        }
        return ResponseResult.success().message("修改成功");
    }







}
